--[[
	TODO
		Pet Misdirection
		Tank Misdirection
		Kill Shot any available mob
		Auto Aspect
		Auto Pet Spec
		Auto Growl
]]
function ccR_Hunter_StartAttack()
	local vUseFunction = ccRS["Hunter_UseStartAttack"]
	if vUseFunction then
		if ccrValidTarget() then
			if ccrPetIsAlive() then
				ccrPetAttack(Target)
			end
			StartAttack()
		end
	end
end

vLast_Auto_Aspect_Checked = 0.1
function ccR_Hunter_Auto_Aspect()
	if ccrPlayerIsInControl()
	and not ccrPlayerHasBuff(FEIGN_DEATH)
	and not ccrPlayerHasBuff(STEALTH)
	and not ccrPlayerHasBuff(CAMOFLAGUE)
	and not ccrPlayerIsCastingOrChanneling()
	and (ccrPlayerHasSpell(ASPECT_OF_THE_HAWK) or ccrPlayerHasSpell(ASPECT_OF_THE_IRON_HAWK))
	and ccrPlayerHasSpell(ASPECT_OF_THE_CHEETAH)
	and (GetTime()-vLast_Auto_Aspect_Checked) > 1.0
	and ((ccRS["Hunter_UseAutoAspectSoloOnly"] and PlayerIsSOLO()) or not ccRS["Hunter_UseAutoAspectSoloOnly"])
	then
		if (ccrPlayerIsInCombat() or (ccrStart and ccrValidTarget()) or IsMounted() == 1)
		and (
			(not ccrPlayerHasTalent(ASPECT_OF_THE_IRON_HAWK) and not ccrPlayerHasBuff(ASPECT_OF_THE_HAWK))
			or
			(ccrPlayerHasTalent(ASPECT_OF_THE_IRON_HAWK) and not ccrPlayerHasBuff(ASPECT_OF_THE_IRON_HAWK))
			)
		and ccrSpellIsReady(ASPECT_OF_THE_HAWK)
		then
			ccrCast(ASPECT_OF_THE_HAWK)
			return
		end

		if (not ccrPlayerIsInCombat() and not ccrValidTarget())
		and ccrSpellIsReady(ASPECT_OF_THE_CHEETAH)
		and not ccrPlayerHasBuff(ASPECT_OF_THE_CHEETAH)
		and not ccrPlayerHasBuff(ASPECT_OF_THE_PACK)
		and IsMounted() == nil
		then
			ccrCast(ASPECT_OF_THE_CHEETAH)
			return
		end
		vLast_Auto_Aspect_Checked = GetTime()
		return
	end
	return
end

function ccR_Hunter_Auto_Pet_Spec()
	if PlayerIsSOLO() 
	and (v_Last_Pet_Spec_Check == nil or GetTime()-v_Last_Pet_Spec_Check > 5)
	then
		if ccrPetSpec() ~= 2 then SetSpecialization(2, true) end;
		v_Last_Pet_Spec_Check = GetTime()
	end
	if PlayerIsInDungeon()
	or PlayerIsInRaid()
	or ccrPlayerIsInGroup()
	and (v_Last_Pet_Spec_Check == nil or GetTime()-v_Last_Pet_Spec_Check > 5)
	then
		if ccrPetSpec() ~= 1 then SetSpecialization(1, true) end;
		v_Last_Pet_Spec_Check = GetTime()
	end
end

function ccR_Hunter_Auto_Pet_Growl()
	local autocastAllowed, autocastEnabled = GetSpellAutocast("Growl", "Pet")
	if (not PlayerIsSOLO() or ccrPlayerIsInGroup()) then
		if autocastEnabled == 1 then
			DisableSpellAutocast("Growl");
		end
	else
		if autocastEnabled == nil then
			EnableSpellAutocast("Growl");
		end
	end
end

function ccR_Hunter_Heart_of_the_Phoenix()
	local vUseAbility = ccRS["Hunter_UseHeartOfThePhoenix"]
	if vUseAbility
	and ccrPlayerHasSpell(HEART_OF_THE_PHOENIX)
	and ccrSpellIsReady(HEART_OF_THE_PHOENIX)
	and ccrPlayerHasPet()
	and ccrPetHealth() == 0
	and not ccrPlayerHasBuff(58984) -- Shadowmeld
	and not ccrPlayerHasBuff(FEIGN_DEATH)
	then
		ccrCast(HEART_OF_THE_PHOENIX)
		return
	end
	return
end

vLast_Serpent_Sting_All_Check = 0.1
function ccR_Hunter_Serpent_Sting_All()
	local vUseAbility = ccRS["Hunter_UseSerpentStingAll"]
	local v 
	if vUseAbility
	and ccrPlayerHasSpell(SERPENT_STING)
	and ccrSpellIsReady(SERPENT_STING)
	and (GetTime()-vLast_Serpent_Sting_All_Check) > 1.5
	then
		local x
		if # tblPGetNearbyEnemies > 0 then
			for x = 0, # tblPGetNearbyEnemies do
				local obj = tblPGetNearbyEnemies[x]
				local vSpell = GetSpellInfo(SERPENT_STING)
				if obj
				and obj:Exists()
				and (obj:IsTappedByPlayer() or (obj:GetTarget():Exists() and obj:GetTarget():GetName() == ccrPlayerName()))
				and obj:GetAura(vSpell) == nil
				and obj:InLineOfSight()
				and ccrSpellCanAttack(SERPENT_STING, obj)
				and not ccrIgnoreNPCWithAura(obj)
				and Player:IsFacing(obj)
				then
					obj:CastSpellByName(GetSpellInfo(SERPENT_STING))
					vLast_Serpent_Sting_All_Check = GetTime()
					return
				end
			end
		end
		return
	end
end

function ccR_Hunter_Kill_Shot_Anyone()
	local vUseAbility = ccRS["Hunter_UseKillShotAnyone"]
	if vUseAbility
	and ccrPlayerHasSpell(KILL_SHOT)
	and not onCD(KILL_SHOT)
	then
		local x
		if # tblPGetNearbyEnemies > 0 then
			for x = 0, # tblPGetNearbyEnemies do
				local obj = tblPGetNearbyEnemies[x]
				if obj
				and obj:Exists()
				and (obj:IsTappedByPlayer() or (obj:GetTarget():Exists() and obj:GetTarget():GetName() == ccrPlayerName()))
				and obj:InLineOfSight()
				and ccrUnitHealth(obj) <= 20
				and Player:IsFacing(obj)
				then
					if ccrPlayerIsCastingOrChanneling() then SpellStopCasting() end
					if ccrPlayerIsCastingOrChanneling() then SpellStopCasting() end
					obj:CastSpellByName(GetSpellInfo(KILL_SHOT))
				end
			end
		end
	end
end